rm(list=ls())
require(tidyverse)
require(plyr)
require(dplyr)
require(scales)
require(rmeta)
files = list.files('results')[grepl("_gender",list.files('results'))]


results = as.data.frame(matrix(nrow = 0, ncol = 8))
colnames(results) = c("coef", "se", "race", "dv", "p value", "endog", "state", 'gender')



for(f in files){
  load(paste0("results/", f))
  holder = as.data.frame(matrix(nrow = 42, ncol = 8))
  colnames(holder) = c("coef", "se", "race", "dv", "p value", "endog", "state", 'gender')
  
  holder$coef = c(ivmod1_white_male$cace,  ivmod1_black_male$cace, ivmod1_hispanic_male$cace,
                   ivmod2_white_male$cace,  ivmod2_black_male$cace, ivmod2_hispanic_male$cace,
                   ivmod3_white_male$cace,  ivmod3_black_male$cace, ivmod3_hispanic_male$cace,
                   ivmod4_white_male$cace,  ivmod4_black_male$cace, ivmod4_hispanic_male$cace,
                   ivmod5_white_male$cace,  ivmod5_black_male$cace, ivmod5_hispanic_male$cace,
                   ivmod6_white_male$cace,  ivmod6_black_male$cace, ivmod6_hispanic_male$cace,
                   ivmod_rep_white_male$cace,  ivmod_rep_black_male$cace, ivmod_rep_hispanic_male$cace,
                  
                   
                   
                  
                  
                  ivmod1_white_female$cace,  ivmod1_black_female$cace, ivmod1_hispanic_female$cace,
                  ivmod2_white_female$cace,  ivmod2_black_female$cace, ivmod2_hispanic_female$cace,
                  ivmod3_white_female$cace,  ivmod3_black_female$cace, ivmod3_hispanic_female$cace,
                  ivmod4_white_female$cace,  ivmod4_black_female$cace, ivmod4_hispanic_female$cace,
                  ivmod5_white_female$cace,  ivmod5_black_female$cace, ivmod5_hispanic_female$cace,
                  ivmod6_white_female$cace,  ivmod6_black_female$cace, ivmod6_hispanic_female$cace,
                  ivmod_rep_white_female$cace,  ivmod_rep_black_female$cace, ivmod_rep_hispanic_female$cace
                  
                  
                  
                  
                   
                   
                   
                   
                   
                   )
  
  holder$se = c(ivmod1_white_male$se,  ivmod1_black_male$se, ivmod1_hispanic_male$se,
                ivmod2_white_male$se,  ivmod2_black_male$se, ivmod2_hispanic_male$se,
                ivmod3_white_male$se,  ivmod3_black_male$se, ivmod3_hispanic_male$se,
                ivmod4_white_male$se,  ivmod4_black_male$se, ivmod4_hispanic_male$se,
                ivmod5_white_male$se,  ivmod5_black_male$se, ivmod5_hispanic_male$se,
                ivmod6_white_male$se,  ivmod6_black_male$se, ivmod6_hispanic_male$se,
                ivmod_rep_white_male$se,  ivmod_rep_black_male$se, ivmod_rep_hispanic_male$se,
                
                
                
               
                ivmod1_white_female$se,  ivmod1_black_female$se, ivmod1_hispanic_female$se,
                ivmod2_white_female$se,  ivmod2_black_female$se, ivmod2_hispanic_female$se,
                ivmod3_white_female$se,  ivmod3_black_female$se, ivmod3_hispanic_female$se,
                ivmod4_white_female$se,  ivmod4_black_female$se, ivmod4_hispanic_female$se,
                ivmod5_white_female$se,  ivmod5_black_female$se, ivmod5_hispanic_female$se,
                ivmod6_white_female$se,  ivmod6_black_female$se, ivmod6_hispanic_female$se,
                ivmod_rep_white_female$se,  ivmod_rep_black_female$se, ivmod_rep_hispanic_female$se
                
                
                
                
                 
                 
                 
  )
  
  holder$`p value` = c(ivmod1_white_male$summary$coefficients[2,4],  ivmod1_black_male$summary$coefficients[2,4], ivmod1_hispanic_male$summary$coefficients[2,4],
                       ivmod2_white_male$summary$coefficients[2,4],  ivmod2_black_male$summary$coefficients[2,4], ivmod2_hispanic_male$summary$coefficients[2,4],
                       ivmod3_white_male$summary$coefficients[2,4],  ivmod3_black_male$summary$coefficients[2,4], ivmod3_hispanic_male$summary$coefficients[2,4],
                       ivmod4_white_male$summary$coefficients[2,4],  ivmod4_black_male$summary$coefficients[2,4], ivmod4_hispanic_male$summary$coefficients[2,4],
                       ivmod5_white_male$summary$coefficients[2,4],  ivmod5_black_male$summary$coefficients[2,4], ivmod5_hispanic_male$summary$coefficients[2,4],
                       ivmod6_white_male$summary$coefficients[2,4],  ivmod6_black_male$summary$coefficients[2,4], ivmod6_hispanic_male$summary$coefficients[2,4],
                       ivmod_rep_white_male$summary$coefficients[2,4],  ivmod_rep_black_male$summary$coefficients[2,4], ivmod_rep_hispanic_male$summary$coefficients[2,4],
                       
                       
                       
                       ivmod1_white_female$summary$coefficients[2,4],  ivmod1_black_female$summary$coefficients[2,4], ivmod1_hispanic_female$summary$coefficients[2,4],
                       ivmod2_white_female$summary$coefficients[2,4],  ivmod2_black_female$summary$coefficients[2,4], ivmod2_hispanic_female$summary$coefficients[2,4],
                       ivmod3_white_female$summary$coefficients[2,4],  ivmod3_black_female$summary$coefficients[2,4], ivmod3_hispanic_female$summary$coefficients[2,4],
                       ivmod4_white_female$summary$coefficients[2,4],  ivmod4_black_female$summary$coefficients[2,4], ivmod4_hispanic_female$summary$coefficients[2,4],
                       ivmod5_white_female$summary$coefficients[2,4],  ivmod5_black_female$summary$coefficients[2,4], ivmod5_hispanic_female$summary$coefficients[2,4],
                       ivmod6_white_female$summary$coefficients[2,4],  ivmod6_black_female$summary$coefficients[2,4], ivmod6_hispanic_female$summary$coefficients[2,4],
                       ivmod_rep_white_female$summary$coefficients[2,4],  ivmod_rep_black_female$summary$coefficients[2,4], ivmod_rep_hispanic_female$summary$coefficients[2,4]
                   
                       
                        
                        
  )
  
  ##############
  holder$race = c(rep(c("white","black", "hispanic"), 14))
  holder$gender = c(rep('male', 21), rep('female', 21))
  
  holder$dv = c(c(rep("Vote 2016", 3),
                 rep("Vote 2014", 3),
                 rep("Vote 2012", 3),
                 rep("Vote 2010", 3),
                 rep("Democrat", 3),
                 rep("Registered", 3),
                 rep("Republican", 3)),
                 
                
                
                c(rep("Vote 2016", 3),
                  rep("Vote 2014", 3),
                  rep("Vote 2012", 3),
                  rep("Vote 2010", 3),
                  rep("Democrat", 3),
                  rep("Registered", 3),
                  rep("Republican", 3))
                 
                 )
  
  
  holder$endog = c(rep("Vote 2008", 7*3),
                   rep("Vote 2008", 7*3))
  holder$state = sub("results_","", sub("_gender.Rdata", "", f))

  
  results = rbind(results, holder)
  
  print(f); print(nrow(results))
}


rm(list=ls()[grepl('mod',ls())])







ggsave(filename = 'figures/FigS21.pdf', width = 9, height = 5, 
plot = results %>%
  mutate(race=Hmisc::capitalize(race), gender = Hmisc::capitalize(gender))%>%
  filter(endog=='Vote 2008', grepl('Vote', dv))%>%
  mutate(race = factor(race, levels = c('Hispanic','White',  'Black')))%>%
  ggplot(aes(x=coef, y = ..scaled..,weight = 1/(se^2), group = race, fill = race,linetype=race))+
  geom_density(alpha=.5)+
  xlab('State-Level Effects')+
  theme_minimal()+
  guides(fill = guide_legend(reverse = TRUE), linetype = guide_legend(reverse = TRUE))+
  scale_linetype_manual(values = c('dotted', 'dashed', 'solid'))+
  scale_fill_manual(values = c('grey','white',  'black'))+
  xlim(-0.1,.5)+
  theme(legend.title=element_blank(), legend.position = 'bottom', legend.spacing.x = unit(.25, 'cm'),
        axis.title.y=element_blank(), axis.text.y=element_blank(),
        axis.ticks.y=element_blank())+  facet_wrap(dv ~ gender, nrow = 4)
)


pid.states = read_csv('state_fips.csv')%>%
  filter(record_pid==1)%>%
  pull(state_abbr)%>%
  tolower()

ggsave(filename = 'figures/FigS23.pdf', width = 9, height = 5,
plot = results %>%
  mutate(race=Hmisc::capitalize(race), gender = Hmisc::capitalize(gender),
         dv=factor(dv, levels = c("Vote 2016",   "Vote 2014"  , "Vote 2012" ,  "Vote 2010" , "Registered" , "Democrat" ,   
                                  "Republican", "Major Party")))%>%
  filter(endog=='Vote 2008', dv%in%c('Registered', 'Democrat', 'Republican'))%>%
  mutate(race = factor(race, levels = c('Hispanic','White',  'Black')),
         coef = ifelse(dv %in% c('Democrat', 'Republican') & !state %in% pid.states, NA, coef),
         se = ifelse(dv %in% c('Democrat', 'Republican') & !state %in% pid.states, NA, se))%>%
  ggplot(aes(x=coef, y = ..scaled..,weight = 1/(se^2), group = race, fill = race,linetype=race))+
  geom_density(alpha=.5)+
  xlab('State-Level Effects')+
  theme_minimal()+
  guides(fill = guide_legend(reverse = TRUE), linetype = guide_legend(reverse = TRUE))+
  scale_linetype_manual(values = c('dotted', 'dashed', 'solid'))+
  scale_fill_manual(values = c('grey','white',  'black'))+
  xlim(-0.3,1)+
  ylab('Density')+
  theme(legend.title=element_blank(), legend.position = 'bottom', legend.spacing.x = unit(.25, 'cm'),
        axis.title.y=element_blank(), axis.text.y=element_blank(),
        axis.ticks.y=element_blank())+  facet_wrap(dv ~ gender, nrow =3)
)

## Weighted averages

data = results %>%
  filter(!dv %in% c('Democrat','Republican'))

predictions_female_white = as.data.frame(matrix(NA, ncol = 6))
predictions_female_black = as.data.frame(matrix(NA, ncol = 6))
predictions_female_hispanic = as.data.frame(matrix(NA, ncol = 6))

predictions_male_white = as.data.frame(matrix(NA, ncol = 6))
predictions_male_black = as.data.frame(matrix(NA, ncol = 6))
predictions_male_hispanic = as.data.frame(matrix(NA, ncol = 6))


colnames(predictions_female_white) = c("ev", "dv", "coef", "se", "race", "gender")
colnames(predictions_female_black) = c("ev", "dv", "coef", "se", "race", "gender")
colnames(predictions_female_hispanic) = c("ev", "dv", "coef", "se", "race", "gender")


colnames(predictions_male_white) = c("ev", "dv", "coef", "se", "race", "gender")
colnames(predictions_male_black) = c("ev", "dv", "coef", "se", "race", "gender")
colnames(predictions_male_hispanic) = c("ev", "dv", "coef", "se", "race", "gender")


predictions_female_white$race = "white"
predictions_female_black$race = "black"
predictions_female_hispanic$race = "hispanic"

predictions_male_white$race = "white"
predictions_male_black$race = "black"
predictions_male_hispanic$race = "hispanic"



predictions_female_white$gender = "female"
predictions_female_black$gender = "female"
predictions_female_hispanic$gender = "female"

predictions_male_white$gender = "male"
predictions_male_black$gender = "male"
predictions_male_hispanic$gender = "male"

for(i in 1:length(unique(data$endog))){
  for(j in 1:length(unique(data$dv))){
    if(length(data$dv[data$dv == unique(data$dv)[j] & data$endog==unique(data$endog)[i]]) > 0){
      
      preds_female_white = meta.maker(data$coef[data$endog == unique(data$endog)[i] & data$race == "white" & data$gender == "female" & data$dv == unique(data$dv)[j] ], data$se[data$endog == unique(data$endog)[i] & data$race == "white" & data$gender == "female"& data$dv == unique(data$dv)[j]])
      predictions_female_white = rbind(predictions_female_white, c(unique(data$endog)[i], unique(data$dv)[j], preds_female_white$est, preds_female_white$se, "white", "female"))
      
      preds_female_black = meta.maker(data$coef[data$endog == unique(data$endog)[i] & data$race == "black" & data$gender == "female"& data$dv == unique(data$dv)[j]], data$se[data$endog == unique(data$endog)[i] & data$race == "black" & data$gender == "female"& data$dv == unique(data$dv)[j]])
      predictions_female_black = rbind(predictions_female_black, c(unique(data$endog)[i], unique(data$dv)[j], preds_female_black$est, preds_female_black$se, "black", "female"))
      
      preds_female_hispanic = meta.maker(data$coef[data$endog == unique(data$endog)[i] & data$race == "hispanic" & data$gender == "female" & data$dv == unique(data$dv)[j]], data$se[data$endog == unique(data$endog)[i] & data$race == "hispanic" & data$gender == "female"& data$dv == unique(data$dv)[j]])
      predictions_female_hispanic = rbind(predictions_female_hispanic, c(unique(data$endog)[i], unique(data$dv)[j], preds_female_hispanic$est, preds_female_hispanic$se, "hispanic", "female"))
      
      preds_male_white = meta.maker(data$coef[data$endog == unique(data$endog)[i] & data$race == "white" & data$gender == "male" & data$dv == unique(data$dv)[j] ], data$se[data$endog == unique(data$endog)[i] & data$race == "white" & data$gender == "male"& data$dv == unique(data$dv)[j]])
      predictions_male_white = rbind(predictions_male_white, c(unique(data$endog)[i], unique(data$dv)[j], preds_male_white$est, preds_male_white$se, "white", "male"))
      
      preds_male_black = meta.maker(data$coef[data$endog == unique(data$endog)[i] & data$race == "black" & data$gender == "male"& data$dv == unique(data$dv)[j]], data$se[data$endog == unique(data$endog)[i] & data$race == "black" & data$gender == "male"& data$dv == unique(data$dv)[j]])
      predictions_male_black = rbind(predictions_male_black, c(unique(data$endog)[i], unique(data$dv)[j], preds_male_black$est, preds_male_black$se, "black", "male"))
      
      preds_male_hispanic = meta.maker(data$coef[data$endog == unique(data$endog)[i] & data$race == "hispanic" & data$gender == "male" & data$dv == unique(data$dv)[j]], data$se[data$endog == unique(data$endog)[i] & data$race == "hispanic" & data$gender == "male"& data$dv == unique(data$dv)[j]])
      predictions_male_hispanic = rbind(predictions_male_hispanic, c(unique(data$endog)[i], unique(data$dv)[j], preds_male_hispanic$est, preds_male_hispanic$se, "hispanic", "male"))
      
      
      
      
      
      
    }
    
  }
  
}
meta_plot=rbind(predictions_female_white, predictions_female_black, predictions_female_hispanic,
                predictions_male_white, predictions_male_black, predictions_male_hispanic)
for(i in 3:(ncol(meta_plot)-2)){
  meta_plot[,i] = as.numeric(meta_plot[,i])
}
meta_plot$se =as.numeric(meta_plot$se)
meta_plot = meta_plot[!is.na(meta_plot$ev),]
meta_plot = meta_plot[!is.nan(meta_plot$coef),]



holder = meta_plot



data = results %>%
  filter(dv %in% c('Democrat','Republican') & state %in% pid.states)

predictions_female_white = as.data.frame(matrix(NA, ncol = 6))
predictions_female_black = as.data.frame(matrix(NA, ncol = 6))
predictions_female_hispanic = as.data.frame(matrix(NA, ncol = 6))

predictions_male_white = as.data.frame(matrix(NA, ncol = 6))
predictions_male_black = as.data.frame(matrix(NA, ncol = 6))
predictions_male_hispanic = as.data.frame(matrix(NA, ncol = 6))


colnames(predictions_female_white) = c("ev", "dv", "coef", "se", "race", "gender")
colnames(predictions_female_black) = c("ev", "dv", "coef", "se", "race", "gender")
colnames(predictions_female_hispanic) = c("ev", "dv", "coef", "se", "race", "gender")


colnames(predictions_male_white) = c("ev", "dv", "coef", "se", "race", "gender")
colnames(predictions_male_black) = c("ev", "dv", "coef", "se", "race", "gender")
colnames(predictions_male_hispanic) = c("ev", "dv", "coef", "se", "race", "gender")


predictions_female_white$race = "white"
predictions_female_black$race = "black"
predictions_female_hispanic$race = "hispanic"

predictions_male_white$race = "white"
predictions_male_black$race = "black"
predictions_male_hispanic$race = "hispanic"



predictions_female_white$gender = "female"
predictions_female_black$gender = "female"
predictions_female_hispanic$gender = "female"

predictions_male_white$gender = "male"
predictions_male_black$gender = "male"
predictions_male_hispanic$gender = "male"

for(i in 1:length(unique(data$endog))){
  for(j in 1:length(unique(data$dv))){
    if(length(data$dv[data$dv == unique(data$dv)[j] & data$endog==unique(data$endog)[i]]) > 0){
      
      preds_female_white = meta.maker(data$coef[data$endog == unique(data$endog)[i] & data$race == "white" & data$gender == "female" & data$dv == unique(data$dv)[j] ], data$se[data$endog == unique(data$endog)[i] & data$race == "white" & data$gender == "female"& data$dv == unique(data$dv)[j]])
      predictions_female_white = rbind(predictions_female_white, c(unique(data$endog)[i], unique(data$dv)[j], preds_female_white$est, preds_female_white$se, "white", "female"))
      
      preds_female_black = meta.maker(data$coef[data$endog == unique(data$endog)[i] & data$race == "black" & data$gender == "female"& data$dv == unique(data$dv)[j]], data$se[data$endog == unique(data$endog)[i] & data$race == "black" & data$gender == "female"& data$dv == unique(data$dv)[j]])
      predictions_female_black = rbind(predictions_female_black, c(unique(data$endog)[i], unique(data$dv)[j], preds_female_black$est, preds_female_black$se, "black", "female"))
      
      preds_female_hispanic = meta.maker(data$coef[data$endog == unique(data$endog)[i] & data$race == "hispanic" & data$gender == "female" & data$dv == unique(data$dv)[j]], data$se[data$endog == unique(data$endog)[i] & data$race == "hispanic" & data$gender == "female"& data$dv == unique(data$dv)[j]])
      predictions_female_hispanic = rbind(predictions_female_hispanic, c(unique(data$endog)[i], unique(data$dv)[j], preds_female_hispanic$est, preds_female_hispanic$se, "hispanic", "female"))
      
      preds_male_white = meta.maker(data$coef[data$endog == unique(data$endog)[i] & data$race == "white" & data$gender == "male" & data$dv == unique(data$dv)[j] ], data$se[data$endog == unique(data$endog)[i] & data$race == "white" & data$gender == "male"& data$dv == unique(data$dv)[j]])
      predictions_male_white = rbind(predictions_male_white, c(unique(data$endog)[i], unique(data$dv)[j], preds_male_white$est, preds_male_white$se, "white", "male"))
      
      preds_male_black = meta.maker(data$coef[data$endog == unique(data$endog)[i] & data$race == "black" & data$gender == "male"& data$dv == unique(data$dv)[j]], data$se[data$endog == unique(data$endog)[i] & data$race == "black" & data$gender == "male"& data$dv == unique(data$dv)[j]])
      predictions_male_black = rbind(predictions_male_black, c(unique(data$endog)[i], unique(data$dv)[j], preds_male_black$est, preds_male_black$se, "black", "male"))
      
      preds_male_hispanic = meta.maker(data$coef[data$endog == unique(data$endog)[i] & data$race == "hispanic" & data$gender == "male" & data$dv == unique(data$dv)[j]], data$se[data$endog == unique(data$endog)[i] & data$race == "hispanic" & data$gender == "male"& data$dv == unique(data$dv)[j]])
      predictions_male_hispanic = rbind(predictions_male_hispanic, c(unique(data$endog)[i], unique(data$dv)[j], preds_male_hispanic$est, preds_male_hispanic$se, "hispanic", "male"))
      
      
      
      
      
      
    }
    
  }
  
}
meta_plot=rbind(predictions_female_white, predictions_female_black, predictions_female_hispanic,
                predictions_male_white, predictions_male_black, predictions_male_hispanic)
for(i in 3:(ncol(meta_plot)-2)){
  meta_plot[,i] = as.numeric(meta_plot[,i])
}
meta_plot$se =as.numeric(meta_plot$se)
meta_plot = meta_plot[!is.na(meta_plot$ev),]
meta_plot = meta_plot[!is.nan(meta_plot$coef),]

meta_plot = rbind(holder,meta_plot)



plt = meta_plot%>%
  mutate(race = factor(Hmisc::capitalize(race), levels = c('Black', 'White', 'Hispanic')),
         gender =Hmisc::capitalize(gender),
         dv = factor(dv, levels = c("Vote 2010", "Vote 2012", "Vote 2014", "Vote 2016", 'Registered', 'Democrat', 'Republican')))

ggsave(filename = "figures/FigS20.pdf", width=9,height=5,
plot = ggplot(data = plt[plt$dv %in% c("Vote 2010", "Vote 2012", "Vote 2014", "Vote 2016"),], aes(x = dv, y = coef, color = race, fill = race)) +
  geom_hline(yintercept = 0, linetype = 'dotted', color = 'grey60') +
  geom_point(shape=21,size = 6, position=position_dodge(.5), alpha = .75) + xlab('') +
  ylab('Effect of 2008 Voting on Future Voting') + 
  geom_errorbar(aes(ymin=coef-1.96*se, ymax=coef+1.96*se, linetype = race), width=0,
                position=position_dodge(.5)) +
  scale_color_manual(values = c("black", "black", "black"))+
  scale_fill_manual(values = c("black", "white", "grey"))+
  scale_linetype_manual(values = c('solid', 'dotdash', 'dashed'))+
  theme_minimal()+
  guides(shape=F)+
  scale_x_discrete(labels = c("2010", "2012", "2014", "2016"))+
  theme(axis.text.y=element_text(face='bold', size =14), axis.title.y=element_text(face='bold', size =14), 
        axis.text.x = element_text(face="bold", size =14), legend.text=element_text(face='bold', size =14),
        legend.position="bottom", legend.spacing.x = unit(.25, 'cm'),legend.title = element_blank(), legend.key.width=unit(4,"line"))+
  facet_grid(.~gender)+
  NULL
)


ggsave(filename = "figures/FigS22.pdf", width=9,height=5,
plot = ggplot(data = plt[plt$dv %in% c("Registered", 'Democrat', 'Republican'),], aes(x = dv, y = coef, color = race, fill = race)) +
  geom_hline(yintercept = 0, linetype = 'dotted', color = 'grey60') +
  geom_point(shape=21,size = 6, position=position_dodge(.5), alpha = .75) + xlab('') +
  ylab('Effect of 2008 Voting on Registration') + 
  geom_errorbar(aes(ymin=coef-1.96*se, ymax=coef+1.96*se, linetype = race), width=0,
                position=position_dodge(.5)) +
  scale_color_manual(values = c("black", "black", "black"))+
  scale_fill_manual(values = c("black", "white", "grey"))+
  scale_linetype_manual(values = c('solid', 'dotdash', 'dashed'))+
  theme_minimal()+
  scale_x_discrete(labels = c("Registered", "Democrat", 'Republican'))+
  theme(axis.text.y=element_text(face='bold', size =14), axis.title.y=element_text(face='bold', size =14), 
        axis.text.x = element_text(face="bold", size =14), legend.text=element_text(face='bold', size =14),
        legend.position="bottom", legend.spacing.x = unit(.25, 'cm'),legend.title = element_blank(), legend.key.width=unit(4,"line"))+
  facet_grid(.~gender)+
  NULL
)

